package org.example.managere.controller.feibiao;

import org.example.managere.dto.feibiao.AmArchivesOperationDto;
import org.example.managere.service.feibiao.AmArchivesOperationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 电机运行数据控制器
 */
@RestController
@RequestMapping("/api/feibiao/operation")
public class AmArchivesOperationController {

    @Autowired
    private AmArchivesOperationService operationService;

    /**
     * 获取所有运行数据（分页）
     */
    @GetMapping
    public ResponseEntity<Page<AmArchivesOperationDto>> findAll(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(defaultValue = "operationId,asc") String sort) {
        String[] sortParams = sort.split(",");
        Sort.Direction direction = sortParams.length > 1 && sortParams[1].equalsIgnoreCase("desc") 
            ? Sort.Direction.DESC 
            : Sort.Direction.ASC;
        PageRequest pageRequest = PageRequest.of(page, size, Sort.by(direction, sortParams[0]));
        return ResponseEntity.ok(operationService.findAll(pageRequest));
    }

    /**
     * 获取所有运行数据
     */
    @GetMapping("/all")
    public ResponseEntity<List<AmArchivesOperationDto>> findAll() {
        return ResponseEntity.ok(operationService.findAll());
    }

    /**
     * 根据ID获取运行数据
     */
    @GetMapping("/{id}")
    public ResponseEntity<AmArchivesOperationDto> findById(@PathVariable Integer id) {
        return operationService.findById(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    /**
     * 创建运行数据
     */
    @PostMapping
    public ResponseEntity<AmArchivesOperationDto> create(@RequestBody AmArchivesOperationDto operationDto) {
        return ResponseEntity.ok(operationService.save(operationDto));
    }

    /**
     * 更新运行数据
     */
    @PutMapping("/{id}")
    public ResponseEntity<AmArchivesOperationDto> update(
            @PathVariable Integer id,
            @RequestBody AmArchivesOperationDto operationDto) {
        operationDto.setOperationId(id);
        return ResponseEntity.ok(operationService.save(operationDto));
    }

    /**
     * 删除运行数据
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable Integer id) {
        operationService.deleteById(id);
        return ResponseEntity.ok().build();
    }

    /**
     * 根据电机ID查询运行数据
     */
    @GetMapping("/by-motor")
    public ResponseEntity<List<AmArchivesOperationDto>> findByMotorId(@RequestParam String motorId) {
        return ResponseEntity.ok(operationService.findByMotorId(motorId));
    }

    /**
     * 获取电机的最新运行数据
     */
    @GetMapping("/latest")
    public ResponseEntity<AmArchivesOperationDto> getLatestOperationByMotorId(@RequestParam String motorId) {
        return operationService.findLatestByMotorId(motorId)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    /**
     * 根据参数范围查询运行数据（如温度、电流等）
     */
    @GetMapping("/by-temperature")
    public ResponseEntity<List<AmArchivesOperationDto>> findByTemperatureRange(
            @RequestParam Double minTemp,
            @RequestParam Double maxTemp) {
        return ResponseEntity.ok(operationService.findByTemperatureBetween(minTemp, maxTemp));
    }
} 